package cn.IT.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.IT.domain.Topic;
import cn.IT.domain.User;
import cn.IT.service.ITopicService;
import cn.IT.util.JsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller()
@Scope("prototype")

public class TopicAction {
    
	private Topic topic;
	private User user;
	private String searchContent;
	private String resultData; // 用于异步传输数据交互
	
	// 在插件包的情况下直接通过名称注入业务层的类
	@Autowired
	private ITopicService topicService;

    JsonConfig jsonConfig = new JsonConfig();  
    
    

	//获取所有帖子
	public String getAllTopics(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Topic> topics = topicService.getAllTopics();
		if(topics != null){
			resultData = "{\"result\": \"success\"" + "," + "\"topics\":" + JSONArray.fromObject(topics,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//删除帖子
	public String delTopic(){
		if (topicService.delTopic(topic,user) == "success"){
			resultData = "{\"result\": \"success\"" +  "}" ;
			System.out.println(resultData);
		}else{
			resultData = "{\"result\": \"error\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//更新消息状态
	public String updateNewsByToUser(){
		if (topicService.updateNewsByToUser(user) == "success"){
			resultData = "{\"result\": \"success\"" +  "}" ;
			System.out.println(resultData);
		}else{
			resultData = "{\"result\": \"error\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//根据标题搜索帖子
	public String searchTopicsByTitle(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Topic> topics = topicService.searchByTitle(searchContent);
		if( topics != null){
			resultData = "{\"result\": \"success\"" + "," + "\"topics\":" + JSONArray.fromObject(topics,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//搜索类型帖子
	public String searchTopicsByType(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Topic> topics = topicService.searchByType(searchContent);
		if( topics != null){
			resultData = "{\"result\": \"success\"" + "," + "\"topics\":" + JSONArray.fromObject(topics,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取用户自己的帖子
	public String findAllTopicsByUser(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Topic> topics = topicService.findAllTopicsByUser(user);
		if( topics != null){
			resultData = "{\"result\": \"success\"" + "," + "\"topics\":" + JSONArray.fromObject(topics,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//新增帖子
	public String addTopic(){
		//用户提出的问题是否存在
		Topic isTitleExist = topicService.isTitleExist(topic);
		System.out.println(isTitleExist);
		if (isTitleExist != null) {
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"该问题已有用户提出，建议先搜索其他用户答案!\""+ "}";
			System.out.println(resultData);
		}
		else{
			topicService.addTopic(topic);
			resultData = "{\"result\": \"success\"" + "}";
			System.out.println(resultData);
		}
		return "success";
	}
    
	//根据id获取帖子
	public String getTopicById(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		Topic comtopic = topicService.getTopicById(topic.getTopicId());
		if(comtopic != null){
			resultData = "{\"result\": \"success\"" + "," + "\"topic\":" + JSONObject.fromObject(comtopic,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取提醒消息(被邀请回答问题的帖子)
	public String getNewsByToUser(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Topic> topics = topicService.getNewsByToUser(user);
		if( topics != null){
			resultData = "{\"result\": \"success\"" + "," + "\"topics\":" + JSONArray.fromObject(topics,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}


	public Topic getTopic() {
		return topic;
	}




	public void setTopic(Topic topic) {
		this.topic = topic;
	}




	public String getSearchContent() {
		return searchContent;
	}

	public void setSearchContent(String searchContent) {
		this.searchContent = searchContent;
	}

	public void setResultData(String resultData) {
		this.resultData = resultData;
	}
	
    @JSON(serialize=false)
	public String getResultData() {
		return resultData;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


}
